package com.ruoyi.web.controller.laiqianba.account;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.account.enums.ActionTypeEnum;
import com.ruoyi.account.enums.ApplyStatusEnum;
import com.ruoyi.account.service.IAccountApplyRecordService;
import com.ruoyi.account.service.IAccountInfoService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.db.domain.AccountApplyRecord;
import com.ruoyi.laiqianba.enums.PostInfoEnum;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.service.IApplyRecordService;
import com.ruoyi.web.wrapper.AccountApplyRecordWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 账户入账申请Controller
 *
 * @author ruoyi
 * @date 2023-08-28
 */
@RestController
@RequestMapping("/account/applyRecord")
public class AccountApplyRecordController extends BaseController {
    @Autowired
    private IAccountApplyRecordService accountApplyRecordService;
    @Autowired
    ISysUserService sysUserService;
    @Autowired
    IApplyRecordService applyRecordService;
    @Autowired
    IAccountInfoService accountInfoService;

    /**
     *
     */
    @GetMapping("/extData")
    public AjaxResult extData() {
        JSONObject returnData = new JSONObject();
        returnData.put("statusMapList", ApplyStatusEnum.getStatusMapList());
        returnData.put("actionTypeList", ActionTypeEnum.getActionTypeMapList());
        returnData.put("mainAccountMapList", accountInfoService.getSimpleMapListByOwnerId());

        // 账户类型
        // 根据用户的
        LoginUser loginUser = getLoginUser();
        returnData.put("itemAccountTypeMapList", PostInfoEnum.getAccountTypeMapList(loginUser.getPostIdList()));

        return AjaxResult.success(returnData);
    }

    /**
     * 查询账户入账申请列表
     */
    @PreAuthorize("@ss.hasPermi('account:applyRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(AccountApplyRecord accountApplyRecord) {

        if (!SecurityUtils.loginUserIsAdmin()) {
            accountApplyRecord.setCreatorId(SecurityUtils.getUserId());
        }

        startPage();
        List<Map<String, Object>> list = accountApplyRecordService.selectAccountApplyRecordMapList(accountApplyRecord);
        list = new AccountApplyRecordWrapper(list).wrap();
        return getDataTable(list);
    }

    /**
     * 导出账户入账申请列表
     */
    @PreAuthorize("@ss.hasPermi('account:applyRecord:export')")
    @Log(title = "账户入账申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccountApplyRecord accountApplyRecord) {
        List<AccountApplyRecord> list = accountApplyRecordService.selectAccountApplyRecordList(accountApplyRecord);
        ExcelUtil<AccountApplyRecord> util = new ExcelUtil<AccountApplyRecord>(AccountApplyRecord.class);
        util.exportExcel(response, list, "账户入账申请数据");
    }

    /**
     * 获取账户入账申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('account:applyRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        AccountApplyRecord accountApplyRecord = accountApplyRecordService.selectAccountApplyRecordById(id);
        Map<String, Object> map = BeanUtil.beanToMap(accountApplyRecord);
        map = new AccountApplyRecordWrapper(map).wrap();
        return success(map);
    }

    /**
     * 新增账户入账申请
     */
    @PreAuthorize("@ss.hasPermi('account:applyRecord:add')")
    @Log(title = "账户入账申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AccountApplyRecord accountApplyRecord) {
        return toAjax(accountApplyRecordService.insertAccountApplyRecord(accountApplyRecord));
    }

    /**
     * 修改账户入账申请
     */
    @PreAuthorize("@ss.hasPermi('account:applyRecord:edit')")
    @Log(title = "账户入账申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AccountApplyRecord accountApplyRecord) {
        return toAjax(accountApplyRecordService.updateAccountApplyRecord(accountApplyRecord));
    }

    /**
     * 删除账户入账申请
     */
    @PreAuthorize("@ss.hasPermi('account:applyRecord:remove')")
    @Log(title = "账户入账申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(accountApplyRecordService.deleteAccountApplyRecordById(id));
    }

    /**
     * 根据账户类型选择，
     * 子账户信息
     */
    @PostMapping("/getActionAndItemAccountData/{accountType}")
    public AjaxResult getActionData(@PathVariable("accountType") String accountType) {
        JSONObject returnData = new JSONObject();

        // 根据账户类型，选择可以操作的步骤
        returnData.put("actionTypeMapList", ActionTypeEnum.getActionTypeMapListByAccount(accountType));

        // 子账户数据信息
        returnData.put("itemAccountInfoMapList", applyRecordService.getItemAccountInfoMapList(accountType));

        return AjaxResult.success(returnData);
    }

    /**
     * 根据账户类型选择，
     * 子账户信息
     */
    @PreAuthorize("@ss.hasPermi('account:applyRecord:applyBill')")
    @PostMapping("/applyBill/{id}")
    public AjaxResult applyBill(@PathVariable("id") Long id) {
        return applyRecordService.applyBill(id, 1);
    }

    @PreAuthorize("@ss.hasPermi('account:applyRecord:refuseBill')")
    @PostMapping("/refuseBill/{id}")
    public AjaxResult refuseBill(@PathVariable("id") Long id) {
        return applyRecordService.applyBill(id, 2);
    }

    /**
     * 根据账户类型选择，
     * 子账户信息
     */
    @PreAuthorize("@ss.hasPermi('account:applyRecord:enterBill')")
    @PostMapping("/enterBill/{id}")
    public AjaxResult enterBill(@PathVariable("id") Long id) {
        LoginUser loginUser = getLoginUser();
        return applyRecordService.enterBill(id, loginUser.getUser());
    }

}
